package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.bnz.pojo.Dish;
import com.bnz.service.DishService;
import com.bnz.service.impl.DishServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/dish")
public class DishServlet extends HttpServlet {

    private DishService dishService;

    @Override
    public void init() throws ServletException {
        dishService = new DishServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        List<Dish> dishes = dishService.findAll();
        String dish = JSON.toJSONString(dishes);
        resp.getWriter().println(dish);
        resp.getWriter().flush();
    }
}
